package com.tianyang.yukuaifuzs.business.main.presenter;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.business.main.contract.SettingCenterContract;
import com.tianyang.yukuaifuzs.business.main.model.MainModel;
import com.tianyang.yukuaifuzs.module.dialog.RemindDialog;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.TwoBtnClickListener;
import com.tianyang.yukuaifuzs.utils.AppContextUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：设置中心
 */

public class SettingCenterPresenter implements SettingCenterContract.Presenter {

    private SettingCenterContract.View mView;

    private CompositeSubscription mSubscription;

    public SettingCenterPresenter(SettingCenterContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void logOff() {
        mView.showLoadingDialog(false,"正在注销...");
        Subscription subscription = MainModel.getInstance().cancellationAccount()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject str) {
                        mView.dismissLoadingDialog();
                        AppContextUtil.exit(mView.getContext());
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void exitApp() {
        new RemindDialog(mView.getContext())
                .title("退出登录")
                .content("是否退出当前账号？")
                .listener(new TwoBtnClickListener() {
                    @Override
                    public void onLeftClick() {

                    }

                    @Override
                    public void onRightClick() {
                        AppContextUtil.exit(mView.getContext());
                    }
                })
                .show();
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
